<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <form class="layui-form layuimini-form" lay-filter="edit-form">
        <input name=id value="" type="number" style="display: none;" />
        <div class="layui-form-item">
            <label class="layui-form-label required">车主名称</label>
            <div class="layui-input-block">
                <input type="text" name="carDriver" autocomplete="off" lay-verify="required" lay-reqtext="车主名称不能为空"
                    placeholder="请输入车主名称" value="" class="layui-input">
                <tip>填写车主名称。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">车牌号</label>
            <div class="layui-input-block">
                <input type="text" name="carNum" autocomplete="off" lay-verify="required" lay-reqtext="车牌号不能为空"
                    placeholder="请输入车牌号" value="" class="layui-input">
                <tip>填写车牌号。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">车主号码</label>
            <div class="layui-input-block">
                <input type="text" name="carPhoneNum" autocomplete="off" lay-verify="required" lay-reqtext="车主号码不能为空"
                    placeholder="请输入车主号码" value="" class="layui-input">
                <tip>填写车主号码。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">车主部门</label>
            <div class="layui-input-block">
                <select id="deptNames" name="carDept" lay-verify="required" lay-search>
                    <option value=""></option>
                </select>
                <tip>填写车主部门。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车辆类型</label>
            <div class="layui-input-block">
                <input type="text" name="carType" autocomplete="off" 
                placeholder="请输入车辆类型" value="" class="layui-input">
                <tip>填写车辆类型。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车辆颜色</label>
            <div class="layui-input-block">
                <input type="text" name="carColor" autocomplete="off" 
                placeholder="请输入车辆颜色" value="" class="layui-input">
                <tip>填写车辆颜色。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车主居住地址</label>
            <div class="layui-input-block">
                <input type="text" name="carAddress" autocomplete="off" 
                placeholder="请输入车主居住地址" value="" class="layui-input">
                <tip>填写车主居住地址。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车主注册时间</label>
            <div class="layui-input-block">
                <input type="text" name="carRegistTime" autocomplete="off" 
                placeholder="请输入车主注册时间" value="" class="layui-input">
                <tip>填写车主注册时间。例如:2000.6.6</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车主驾驶证号码</label>
            <div class="layui-input-block">
                <input type="text" name="carDriverNum" autocomplete="off" 
                placeholder="请输入车主驾驶证号码" value="" class="layui-input">
                <tip>填写车主驾驶证号码。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车主简介</label>
            <div class="layui-input-block">
                <input type="text" name="carRemarks" autocomplete="off" 
                placeholder="请输入车主简介" value="" class="layui-input">
                <tip>填写车主简介。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否隐藏</label>
            <div class="layui-input-block">
                <input type="text" name="isHide" autocomplete="off" 
                placeholder="请输入是否隐藏" value="0" class="layui-input">
                <tip>填写是否隐藏 0不隐藏，1隐藏。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数据排序</label>
            <div class="layui-input-block">
                <input type="number" name="dataSort" autocomplete="off" 
                placeholder="请输入数据排序" value="0" class="layui-input">
                <tip>填写数据排序。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button id="saveBtn" class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存</button>
                <button type="reset" class="layui-btn layui-btn-danger">重置</button>
                <input type="button" class="layui-btn layui-btn-primary goback" value="返回"/>
            </div>
        </div>
    </form>
    <script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../../js/lay-config.js?v=2.0.0" charset="utf-8"></script>
    <script>
        function initView(obj) {
            console.log(obj);
            var findDeptNamesUrl = "/dicms/api/desktop/carnum_datas/findDeptNames";
            var findOneUrl = "/dicms/api/desktop/carnum_datas/findOne";
            var saveOneUrl = "/dicms/api/desktop/carnum_datas/save";
            layui.use(['form','common'], function () {
                var form = layui.form,
                    layer = layui.layer,
                    common = layui.common,
                    $ = layui.$;
                if (parent.tableConfig.editMode == 0) {
                    $.ajax({
                        url: findDeptNamesUrl,
                        data: {},
                        type: 'get',
                        dataType: 'json',
                        success: function (res) {
                            console.log(res);
                            if (res.code !== 0) {
                                layer.msg(res.msg, function () {
                                });
                            } else {
                                let options = '';
                                let deptNames = res.data;
                                options +='<option value=""></option>'
                                deptNames.forEach(function(val){
                                    options+=`<option value="${val.deptName}">${val.deptName}</option>`;
                                });
                                $('#deptNames').html(options);
                                form.val('edit-form', res.data);
                                form.render('select');
                            }
                        },
                        error: function (res) {
                            console.log(res);
                        }
                    });
                } else if (parent.tableConfig.editMode == 1) {
                    console.log(obj.data);
                    let objId = obj.data.id;
                    $.ajax({
                        url: findOneUrl,
                        data: { id: objId },
                        type: 'get',
                        dataType: 'json',
                        success: function (res) {
                            console.log(res);
                            if (res.code !== 0) {
                                layer.msg(res.msg, function () {
                                });
                            } else {
                                let options = '';
                                let deptNames = res.data.deptNames;
                                options +='<option value=""></option>'
                                deptNames.forEach(function(val){
                                    options+=`<option value="${val.deptName}">${val.deptName}</option>`;
                                });
                                $('#deptNames').html(options);
                                form.render('select');
                                form.val('edit-form', res.data);
                            }
                        },
                        error: function (res) {
                            console.log(res);
                        }
                    });

                }

                //监听提交
                form.on('submit(saveBtn)', function (data) {
                    data = data.field;
                    console.log(data);
                    if (data.carDriver.length > 10) {
                        layer.msg('车主名称太长!');
                        return false;
                    }
                    $("#saveBtn").attr('disabled','disabled');
                    $("#saveBtn").addClass("layui-btn-disabled");
                    $.ajax({
                        url: saveOneUrl,
                        data: data,
                        type: 'post',
                        dataType: 'json',
                        success: function (res) {
                            console.log(res);
                            if (res.code !== 0) {
                                layer.msg(res.msg, function () {
                                });
                            } else {
                                common.refreshAndClose();
                            }
                        },
                        error: function (res) {
                            console.log(res);
                            $("#saveBtn").removeAttr('disabled');
                            $("#saveBtn").removeClass("layui-btn-disabled");
                        }
                    });
                    return false;
                });

                $(".goback").click(function (e) {
                    common.closeIframe();
                });

            });
        }
    </script>
</body>

</html>